package com.mine.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.dao.GoodsMapper;
import com.mine.dao.MineGoodsMapper;
import com.mine.dao.MineShopAdsMapper;
import com.mine.dao.MineShopAreaCategoryMapper;
import com.mine.dao.MineShopAreaMapper;
import com.mine.dao.MineShopFunctionMapper;
import com.mine.dao.MineShopInfoMapper;
import com.mine.dao.ShopFunctionMapper;
import com.mine.pojo.GoodsDataInfo;
import com.mine.pojo.MineGoods;
import com.mine.pojo.MineShopAds;
import com.mine.pojo.MineShopAdsExample;
import com.mine.pojo.MineShopArea;
import com.mine.pojo.MineShopAdsExample.Criteria;
import com.mine.pojo.MineShopAreaCategory;
import com.mine.pojo.MineShopAreaCategoryExample;
import com.mine.pojo.MineShopAreaExample;
import com.mine.pojo.MineShopFunction;
import com.mine.pojo.MineShopFunctionExample;
import com.mine.pojo.MineShopInfo;
import com.mine.pojo.MineShopInfoExample;
import com.mine.pojo.MineUser;
import com.mine.pojo.ShopArea;
import com.mine.pojo.ShopAreaData;
import com.mine.pojo.ShopFirstPageInfo;
import com.mine.pojo.ShopFunction;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.vo.GoodsQueryVo;

@Service
public class ShopServiceImpl implements ShopService {

	@Autowired 
	MineGoodsMapper goodsMapper;
	@Autowired
	UserService userService;
	
	@Autowired
	MineShopInfoMapper shopInfoMapper;
	
	@Autowired
	MineShopAdsMapper shopAdsMapper;
	
	@Autowired
	MineShopAreaCategoryMapper areaCategoryMapper;
	
	@Autowired
	MineShopAreaMapper shopAreaMapper;
	
	@Autowired
	ShopFunctionMapper shopFuctionMapper;
	
	@Autowired
	MineShopFunctionMapper mineShopFunctionMapper;
	
	@Autowired
	GoodsMapper gMapper;
	
	/**
	 * 创建店铺
	 */
	@Override
	public Result createShop(String token,MineShopInfo shop) {
		
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		MineShopInfoExample example = new MineShopInfoExample();
		com.mine.pojo.MineShopInfoExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andUserIdEqualTo(user.getId());
		List<MineShopInfo> list = shopInfoMapper.selectByExample(example );
		if (list.size()>0) {
			return Result.success("您已经创建店铺，不能重复创建！");
		}
		//未创建店铺，且用户存在，把数据插入到表中
		shop.setUserId(user.getId());
		shop.setCreateTime(new Date());
		shop.setUpdateTime(new Date());
		
		shopInfoMapper.insertSelective(shop);
		
		//此时生成的shopId已经放入shop对象中了
		user.setShopId(shop.getId());
		//更新redis中user的shopId值
		userService.updateUserInfo(token, user);
		System.out.println("更新 MineShopInfoMapper.xml 需要更新insertSelective"+user.getShopId());
		
		return Result.ok();
		
	}
	
	/**
	 * 更新店铺
	 */
	@Override
	public Result updateShopInfo(String token, MineShopInfo shop) {
		//先查询用户信息
		Result result = userService.getUserByToken(token);
		MineUser user = (MineUser) result.getData();
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//查询该用户的店铺是否存在
		MineShopInfoExample example = new MineShopInfoExample();
		com.mine.pojo.MineShopInfoExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		List<MineShopInfo> selectByExample = shopInfoMapper.selectByExample(example);
		if (selectByExample==null||selectByExample.size()==0) {
			return Result.error("您没有创建店铺！");
		}
		//有店铺 更新店铺信息
		shop.setId(user.getShopId());
		System.out.println("shopID"+user.getShopId());
		shop.setUpdateTime(new Date());
		int updateByPrimaryKeySelective = shopInfoMapper.updateByPrimaryKeySelective(shop);
		System.out.println(shop);
		if (updateByPrimaryKeySelective>0) {
			return Result.ok();
		}
		return Result.error("更新失败！");
	}

	/**
	 * 添加商铺轮播图广告
	 */
	@Override
	public Result addShopAdsItem(String token, MineShopAds ads) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺！");
		}
		ads.setUserId(user.getId());
		ads.setShopId(user.getShopId());
		ads.setCreateTime(new Date());
		ads.setUpdateTime(new Date());
		int i = shopAdsMapper.insertSelective(ads);
		if(i>0){
			return Result.ok();
		}
		return Result.error("添加失败！");
	}

	
	/**
	 * 获取商铺的所有轮播图列表
	 */
	@Override
	public Result getShopAdsList(String token) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，获取失败！");
		}
		MineShopAdsExample example = new MineShopAdsExample();
		Criteria criteria = example.createCriteria();
		criteria.andOfTypeEqualTo((byte) 0); //所属的类型 0默认属于轮播图 1属于单个图片展示
		criteria.andShopIdEqualTo(user.getShopId());
		List<MineShopAds> list = shopAdsMapper.selectByExample(example);
		
		return Result.success(list);
	}

	/**
	 * 更新某一条轮播图item数据
	 */
	@Override
	public Result updateShopAds(String token, MineShopAds ads) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，更新失败！");
		}
		ads.setUserId(user.getId());
		ads.setShopId(user.getShopId());
		ads.setUpdateTime(new Date());
		int keySelective = shopAdsMapper.updateByPrimaryKeySelective(ads);
		if (keySelective>0) {
			return Result.ok();
		}
		return Result.error("更新失败");
	}

	/**
	 * 删除某一条轮播图item数据
	 */
	@Override
	public Result deleteShopAds(String token,MineShopAds ads) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，删除失败！");
		}
		int primaryKey = shopAdsMapper.deleteByPrimaryKey(ads.getId());
		if (primaryKey>0) {
			return Result.ok();
		}
		return Result.error("删除失败");
	}

	/**
	 * 获取商铺信息
	 */
	@Override
	public Result getShopInfo(String token) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，获取失败！");
		}
		MineShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(user.getShopId());
		if (shopInfo!=null) {
			return Result.success(shopInfo);
		}
		return Result.error("查询失败！");
	}

	/**
	 * 添加首页的区域分类
	 */
	@Override
	public Result addShopArea(String token, MineShopAreaCategory shopAreaCategory) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，创建失败！");
		}
		shopAreaCategory.setShopId(user.getShopId());
		shopAreaCategory.setUserId(user.getId());
		shopAreaCategory.setUpdateTime(new Date());
		shopAreaCategory.setCreateTime(new Date());
		int insertSelective = areaCategoryMapper.insertSelective(shopAreaCategory);
		if (insertSelective>0) {
			return Result.ok();
		}
		return Result.error("添加失败");
	}
	
	/**
	 * 更新首页的区域分类
	 */
	@Override
	public Result updateShopArea(String token, MineShopAreaCategory shopAreaCategory) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，更新失败！");
		}
		
		shopAreaCategory.setUpdateTime(new Date());

		int selective = areaCategoryMapper.updateByPrimaryKeySelective(shopAreaCategory);
		if (selective>0) {
			return Result.ok();
		}
		return Result.error("更新失败");
	}
	
	@Override
	public Result deleteShopArea(String token, MineShopAreaCategory shopAreaCategory) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，更新失败！");
		}
		//根据id删除
		areaCategoryMapper.deleteByPrimaryKey(shopAreaCategory.getId());
		//删除mine_shop_area表中category_id中相等的id
		MineShopAreaExample example = new MineShopAreaExample();
		com.mine.pojo.MineShopAreaExample.Criteria criteria = example.createCriteria();
		criteria.andAreaCategoryIdEqualTo(shopAreaCategory.getId());
		shopAreaMapper.deleteByExample(example);
		return Result.ok();
	}
	
	@Override
	public Result addShopAreaGoods(String token, MineShopArea shopArea) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，添加失败！");
		}
		//查询商品表，补全
		MineGoods mineGoods = goodsMapper.selectByPrimaryKey(shopArea.getGoodsId());
		if (mineGoods==null) {
			return Result.error("添加的商品不存在");
		}
		//查询分类表，查看该分类是否存在
		//先查这个分类id是否存在
		MineShopAreaCategory category = areaCategoryMapper.selectByPrimaryKey(shopArea.getAreaCategoryId());
		if (category==null) {
			return Result.error("分类不存在，添加失败");
		}
		//分转换为元
		double p= mineGoods.getPrice().doubleValue()/100;
		shopArea.setGoodsImgUrl(mineGoods.getGoodsPhotos());
		shopArea.setGoodsPrice(new BigDecimal(p));
		shopArea.setGoodsTitle(mineGoods.getTitle());
		shopArea.setUserId(user.getId());
		shopArea.setShopId(user.getShopId());
		shopArea.setCreateTime(new Date());
		shopArea.setUpdateTime(new Date());
		
		int insertSelective = shopAreaMapper.insertSelective(shopArea);
		if (insertSelective>0) {
			return Result.ok();
		}
		return Result.error("添加失败！");
	}

	@Override
	public Result deleteShopAreaOneGoods(String token, String goodsId, String areaCategoryId) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，添加失败！");
		}
		//先查这个分类id是否存在
		MineShopAreaCategory category = areaCategoryMapper.selectByPrimaryKey(Long.parseLong(areaCategoryId));
		if (category==null) {
			return Result.error("分类不存在，删除失败");
		}
		MineShopAreaExample example =new MineShopAreaExample();
		com.mine.pojo.MineShopAreaExample.Criteria criteria = example.createCriteria();
		criteria.andAreaCategoryIdEqualTo(Long.parseLong(areaCategoryId));
		criteria.andGoodsIdEqualTo(Long.parseLong(goodsId));
		int i = shopAreaMapper.deleteByExample(example);
		if (i>0) {
			return Result.ok();
		}
		return Result.error("删除失败！");
	}

	@Override
	public Result getShopFirstPageInfo(Long shopId) {
		ShopFirstPageInfo firstPageInfo = new ShopFirstPageInfo();
		//根据shopId查询商铺
		MineShopInfo info = shopInfoMapper.selectByPrimaryKey(shopId);
		if (info==null) {
			return Result.error("未创建该店铺！");
		}
		firstPageInfo.setShopNotice(info.getShopNotice());
		//查询商铺轮播图
		MineShopAdsExample example = new MineShopAdsExample();
		Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(shopId);
		List<MineShopAds> list = shopAdsMapper.selectByExample(example);
		firstPageInfo.setShopAds(list);
		//查询在首页商铺展示的商品
		GoodsQueryVo queryVo = new GoodsQueryVo();
		queryVo.setShopId(shopId);
		queryVo.setPresentation(1);
		List<GoodsDataInfo> goodsList = gMapper.findGoodsAll(queryVo);
		//List<GoodsDataInfo> goodsList = new ArrayList<GoodsDataInfo>();
		
		//把相同的categoryId放在同一个分组
		HashSet<Long> hSet = new HashSet<>();
		for (int i = 0; i < goodsList.size(); i++) {
			GoodsDataInfo dataInfo = goodsList.get(i);
			hSet.add(dataInfo.getCategoryId());
		}
		List<ShopAreaData> dataList = new ArrayList<ShopAreaData>();
		for (Long catId : hSet) {
			ShopAreaData areaData = new ShopAreaData();
			List<GoodsDataInfo> tmpList = new ArrayList<GoodsDataInfo>();
			for (int i = 0; i < goodsList.size(); i++) {
				
				GoodsDataInfo gDataInfo = goodsList.get(i);
				if (catId==gDataInfo.getCategoryId()) {
					
					areaData.setTitle(gDataInfo.getCategoryName());
					tmpList.add(gDataInfo);
				}
			}
			areaData.setData(tmpList);
			dataList.add(areaData);
		}
		
//		MineShopAreaCategoryExample areaCategoryExample = new MineShopAreaCategoryExample();
//		com.mine.pojo.MineShopAreaCategoryExample.Criteria createCriteria2 = areaCategoryExample.createCriteria();
//		createCriteria2.andShopIdEqualTo(shopId);
//		List<MineShopAreaCategory> categoryList = areaCategoryMapper.selectByExample(areaCategoryExample); 
//		
//		List<ShopArea> findShopArea = shopFuctionMapper.findShopArea(shopId);
//		List<ShopAreaData> dataList = new ArrayList<ShopAreaData>();
//		for (MineShopAreaCategory mineShopAreaCategory : categoryList) {
//			List<ShopArea> innerfindShopArea = new ArrayList<ShopArea>();
//			for (ShopArea shopArea : findShopArea) {
//				if (mineShopAreaCategory.getId().equals(shopArea.getId())) {
//					innerfindShopArea.add(shopArea);
//				}
//			}
//			if (innerfindShopArea.size()>0) {
//				ShopAreaData areaData = new ShopAreaData();
//				ShopArea a = innerfindShopArea.get(0);
//				areaData.setTitle(a.getAreaName());
//				areaData.setData(innerfindShopArea);
//				dataList.add(areaData);
//			}
//		}
		firstPageInfo.setShopAreas(dataList);
//		MineShopAreaExample mineShopAreaExample = new MineShopAreaExample() ;
//		com.mine.pojo.MineShopAreaExample.Criteria createCriteria = mineShopAreaExample.createCriteria();
//		createCriteria.andShopIdEqualTo(shopId);
//		List<MineShopArea> shopAreasList = shopAreaMapper.selectByExample(mineShopAreaExample);
//		firstPageInfo.setShopAreas(shopAreasList);
		
//		List<ShopArea> findShopArea = shopFuctionMapper.findShopArea(shopId);
		
//		for (int i = 0; i < findShopArea.size(); i++) {
//			List<ShopArea> tmpShopAreaList = new ArrayList<ShopArea>();
//			ShopArea shopArea = findShopArea.get(i);
//			for (int j = 0; j < findShopArea.size(); j++) {
//				ShopArea innershopArea = findShopArea.get(j);
//				if (shopArea.getId().equals(innershopArea.getId())) {
//					ShopAreaData shopAreaData = new ShopAreaData();
//					shopAreaData.setTitle(innershopArea.getAreaName());
//					
//				}
//			}
//		}
		//查询商铺已经开通的功能列表
		MineShopFunctionExample shopFunctionExample = new MineShopFunctionExample();
		com.mine.pojo.MineShopFunctionExample.Criteria createCriteria = shopFunctionExample.createCriteria();
		createCriteria.andShopIdEqualTo(shopId);
		createCriteria.andStatusEqualTo(1);
		List<MineShopFunction> selectByExample = mineShopFunctionMapper.selectByExample(shopFunctionExample);
		
		firstPageInfo.setFunctionItem(selectByExample);
		//查询商铺单张图片
		MineShopAdsExample shopAdsExample = new MineShopAdsExample();
		Criteria criteria2 = shopAdsExample.createCriteria();
		criteria2.andShopIdEqualTo(shopId);
		criteria2.andOfTypeEqualTo((byte) 1);
		List<MineShopAds> adsList = shopAdsMapper.selectByExample(shopAdsExample);
		firstPageInfo.setSingleList(adsList);

		return Result.success(firstPageInfo);
	}

	@Override
	public Result userGetShopInfo(Long shopId) {
		
		MineShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(shopId);
		if (shopInfo!=null) {
			return Result.success(shopInfo);
		}
		return Result.error("查询失败！");
		
	}

	@Override
	public Result getShopFunctionList(String token) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，获取失败！");
		}
		MineShopFunctionExample example = new MineShopFunctionExample();
		com.mine.pojo.MineShopFunctionExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andShopIdEqualTo(user.getShopId());
		createCriteria.andUserIdEqualTo(user.getId());
		//		MineShopAdsExample example = new MineShopAdsExample();
//		Criteria criteria = example.createCriteria();
//		criteria.andOfTypeEqualTo((byte) 0); //所属的类型 0默认属于轮播图 1属于单个图片展示
//		criteria.andShopIdEqualTo(user.getShopId());
//		List<MineShopAds> list = shopAdsMapper.selectByExample(example);
		
		List<MineShopFunction> list = mineShopFunctionMapper.selectByExample(example );
		
		return Result.success(list);
	}

	@Override
	public Result updateShopFunction(String token, MineShopFunction shopFunction) {
		
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		
		mineShopFunctionMapper.updateByPrimaryKeySelective(shopFunction);
		
		return Result.ok();
	}

	

}
